package com.rich.common.util;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:fei
 * @Date:2021/8/19
 */
public class SSOClientUtil {

    public static final String SERVER_URL_PREFIX="http://www.sso.com:8001";//sso模块
    public static final String HOUSE_HOST_URL="http://localhost:7006";//HOUSE模块
    public static final String AREA_HOST_URL="http://localhost:7007";//地区模块
    public static final String BROKER_HOST_URL="http://localhost:7004";//经纪人模块
    public static final String GROUP_HOST_URL="http://localhost:7013";//团购模块
    public static final String LIVE_HOST_URL="http://localhost:7008";//直播模块
    public static final String ORDER_HOST_URL="http://localhost:7010";//订单模块
    public static final String USER_HOST_URL="http://localhost:7005";//用户模块


    public static void redirectToSSOURL(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String redirectUrl= getRedirectUrl(request);
        StringBuilder url = new StringBuilder(50).append(SERVER_URL_PREFIX)
                .append("/checkLogin?redirectUrl=")
                .append(redirectUrl);
        response.sendRedirect(url.toString());
    }

    private static String getRedirectUrl(HttpServletRequest request) {
        // TODO controller的路径重复
        String servletPath = request.getServletPath();
        String[] split = servletPath.split("/");
        if (split[1].equals("broker") || split[1].equals("houesering") ||split[1].equals("audit") ||split[1].equals("house") ||split[1].equals("information") || split[1].equals("want")){
            return HOUSE_HOST_URL+request.getServletPath();
        }
        if (split[1].equals("area") ){
            return AREA_HOST_URL+request.getServletPath();
        }
        if (split[1].equals("broker") || split[1].equals("rentSuccessHouse") ||split[1].equals("sellSuccessHouse") ||split[1].equals("want")){
            return BROKER_HOST_URL+request.getServletPath();
        }
        if (split[1].equals("group")){
            return GROUP_HOST_URL+request.getServletPath();
        }
        if (split[1].equals("live") || split[1].equals("liveFollower") ||split[1].equals("liveMessage")){
            return LIVE_HOST_URL+request.getServletPath();
        }
        if (split[1].equals("user")){
            return USER_HOST_URL+request.getServletPath();
        }if (split[1].equals("order")){
            return ORDER_HOST_URL+request.getServletPath();
        }
        return null;
    }

    public static String getClientLogOutUrl(){
        return HOUSE_HOST_URL+"/logOut";
    }

    public static String getServerLogOutUrl(){
        return SERVER_URL_PREFIX+"/logOut";
    }
}
